@@ -21,6 +21,8 @@ module Agents |
||
| 21 | 21 |
* `this.options(key)` |
| 22 | 22 |
* `this.log(message)` |
| 23 | 23 |
* `this.error(message)` |
| 24 |
+ * `this.escapeHtml(htmlToEscape)` |
|
| 25 |
+ * `this.unescapeHtml(htmlToUnescape)` |
|
| 24 | 26 |
MD |
| 25 | 27 |
|
| 26 | 28 |
def validate_options |
@@ -102,6 +104,8 @@ module Agents |
||
| 102 | 104 |
memory.to_json |
| 103 | 105 |
end |
| 104 | 106 |
end |
| 107 |
+ context["escapeHtml"] = lambda { |a, x| CGI.escapeHTML(x) }
|
|
| 108 |
+ context["unescapeHtml"] = lambda { |a, x| CGI.unescapeHTML(x) }
|
|
| 105 | 109 |
|
| 106 | 110 |
context.eval(code) |
| 107 | 111 |
context.eval("Agent.#{js_function}();")
|
@@ -158,6 +162,14 @@ module Agents |
||
| 158 | 162 |
doError(message); |
| 159 | 163 |
} |
| 160 | 164 |
|
| 165 |
+ Agent.escapeHtml = function(html) {
|
|
| 166 |
+ return escapeHtml(html); |
|
| 167 |
+ } |
|
| 168 |
+ |
|
| 169 |
+ Agent.unescapeHtml = function(html) {
|
|
| 170 |
+ return unescapeHtml(html); |
|
| 171 |
+ } |
|
| 172 |
+ |
|
| 161 | 173 |
Agent.check = function(){};
|
| 162 | 174 |
Agent.receive = function(){};
|
| 163 | 175 |
JS |
@@ -162,6 +162,20 @@ describe Agents::JavaScriptAgent do |
||
| 162 | 162 |
end |
| 163 | 163 |
end |
| 164 | 164 |
|
| 165 |
+ describe "escaping and unescaping HTML" do |
|
| 166 |
+ it "can escape and unescape html with this.escapeHtml and this.unescapeHtml in the javascript environment" do |
|
| 167 |
+ @agent.options['code'] = 'Agent.check = function() { this.createEvent({ escaped: this.escapeHtml(\'test \"escaping\" <characters>\'), unescaped: this.unescapeHtml(\'test "unescaping" <characters>\')}); };'
|
|
| 168 |
+ @agent.save! |
|
| 169 |
+ expect {
|
|
| 170 |
+ expect {
|
|
| 171 |
+ @agent.check |
|
| 172 |
+ }.not_to change { AgentLog.count }
|
|
| 173 |
+ }.to change { Event.count}.by(1)
|
|
| 174 |
+ created_event = @agent.events.last |
|
| 175 |
+ expect(created_event.payload).to eq({ 'escaped' => 'test "escaping" <characters>', 'unescaped' => 'test "unescaping" <characters>'})
|
|
| 176 |
+ end |
|
| 177 |
+ end |
|
| 178 |
+ |
|
| 165 | 179 |
describe "getting incoming events" do |
| 166 | 180 |
it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do |
| 167 | 181 |
event = Event.new |